package com.wx.mall.operate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.wx.mall.common.utils.UploadUtil;
import com.wx.mall.operate.dto.ActivityDto;
import com.wx.mall.operate.entity.Activity;
import com.wx.mall.operate.mapper.ActivityMapper;
import com.wx.mall.operate.service.IActivityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author j
 * @since 2020-05-07
 */
@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements IActivityService {

    @Value("${localhost.file.path}")
    private String filePath;
    @Autowired
    private UploadUtil uploadUtil;
    @Override
    public List<Activity> getList(ActivityDto dto) {

        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());

//        获取查询条件

        QueryWrapper<Activity> wrapper=new QueryWrapper<>();

        if (!dto.getTitle().equals("")){
            wrapper.like("title",dto.getTitle());
        }
        if (!dto.getStatus().equals("")){
            wrapper.like("status",dto.getStatus());
        }


        switch (dto.getSort()){

            case 0:
                wrapper.orderByAsc("title");
                break;
            case 1:
                wrapper.orderByAsc("start_time");
                break;

        };

        List<Activity> list=this.list(wrapper);

        return list;
    };

    @Override
    public Map<String, String> uploadImage(MultipartFile file, String name) {


        return uploadUtil.uploadMD5(file,filePath,name);
    }
}
